package tiepbm.app.connect.resfulwebservice;

import java.io.BufferedInputStream;

import java.util.Arrays;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;

import tiepbm.app.store.object.Obj_Category;
import tiepbm.app.store.object.Obj_User;
import app.store.json.AndroidServiceIteratorProvider;
import app.store.json.JsonEnv;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.ClientFilter;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.spi.service.ServiceFinder;

public class WebserviceConnect {

	private static ClientConfig clientConfig;
	private static Gson gson;

	public WebserviceConnect() {
		clientConfig = new DefaultClientConfig();
		clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
				Boolean.TRUE);

		ServiceFinder
				.setIteratorProvider(new AndroidServiceIteratorProvider<Object>());
		gson = new GsonBuilder().create();
	}

	// static {
	// clientConfig = new DefaultClientConfig();
	// clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
	// Boolean.TRUE);
	//
	// ServiceFinder
	// .setIteratorProvider(new AndroidServiceIteratorProvider<Object>());
	// gson = new GsonBuilder().create();
	// }

	public static ClientResponse post(String url, String requestBody,
			String username, String password) {

		Client client = Client.create();
		if (username != null && password != null) {
			client.addFilter(new HTTPBasicAuthFilter(username, password));
		}
//		client.addFilter(new ClientFilter() {
//			
//			@Override
//			public ClientResponse handle(ClientRequest arg0)
//					throws ClientHandlerException {
//				// TODO Auto-generated method stub
//				return null;
//			}
//		});
		WebResource webResource = client.resource(url);
		ClientResponse response = webResource
				.accept(MediaType.APPLICATION_JSON_TYPE)
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class, requestBody);

		System.out.println(response.toString());
		return response;
	}

	public static ClientResponse put(String url, String requestBody,
			String username, String password) {

		Client client = Client.create();
		if (username != null && password != null) {
			client.addFilter(new HTTPBasicAuthFilter(username, password));
		}

		WebResource webResource = client.resource(url);

		ClientResponse response = webResource
				.accept(MediaType.APPLICATION_JSON_TYPE)
				.type(MediaType.APPLICATION_JSON_TYPE)
				.put(ClientResponse.class, requestBody);

		System.out.println(response.toString());

		return response;
	}

	public static ClientResponse get(String url,
			MultivaluedMap<String, String> params, String username,
			String password) {

		Client client = Client.create();
		if (username != null && password != null) {
			client.addFilter(new HTTPBasicAuthFilter(username, password));
		}

		WebResource webResource = client.resource(url);

		ClientResponse response;
	
		if (params != null)
			response = webResource.queryParams(params)
					.accept(MediaType.APPLICATION_JSON_TYPE)
					.type(MediaType.APPLICATION_JSON_TYPE)
					.get(ClientResponse.class);
		else
			response = webResource.accept(MediaType.APPLICATION_JSON_TYPE)
					.type(MediaType.APPLICATION_JSON_TYPE)
					.get(ClientResponse.class);

		System.out.println(response.toString());

		return response;
	}

	public static Obj_User registerUser(Obj_User userRegister) {

		ClientResponse response = post(JsonEnv.SERVER_URL
				+ JsonEnv.API_USER_SIGN_UP, gson.toJson(userRegister), null,
				null);

		if (response.getStatus() != 201 && response.getStatus() != 200) {
			System.out.println("registerUser() error: "
					+ response.getEntity(String.class));

			return null;
		}

		Obj_User u = gson.fromJson(response.getEntity(String.class), Obj_User.class);

		return u;
	}

	public static int userLogin(String username, String password) {
		ClientResponse response = get(JsonEnv.SERVER_URL
				+ JsonEnv.API_USER_LOGIN, null, username, password);

		return response.getStatus();
	}
	public static List<Obj_Category> getCategory() {
		ClientResponse response = get(JsonEnv.SERVER_URL + JsonEnv.API_CATEGORY, null, null, null);
		
		if (response.getStatus() != 201 && response.getStatus() != 200) {
			System.out.println("registerUser() error: " + response.getEntity(String.class));
			
			return null;
		}
		
		Obj_Category[] catList = gson.fromJson(response.getEntity(String.class), Obj_Category[].class);
		
		return Arrays.asList(catList);
	}
}
